<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item >
      <el-form-item label="服务类型">
        <el-select v-model="queryParams.serveTypeId" filterable clearable placeholder="请选择服务类型">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="queryParams.activeStatus" @change="handleQuery">
          <el-radio label="">全部</el-radio>
          <el-radio label="2">启用</el-radio>
          <el-radio label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务编码" align="center" prop="code" />
      <el-table-column label="服务类型" align="center" prop="serveTypeList[0].name">
      </el-table-column>
      <el-table-column label="服务名称" align="center" prop="name" />
      <el-table-column label="服务图标" align="center" prop="serveItemIcon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.serveItemIcon" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="服务图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="服务描述" align="center" prop="description" />
      <el-table-column label="参考价格" align="center" prop="referencePrice" />
      <el-table-column label="排序字段" align="center" prop="sortNum" />
      <el-table-column label="活动状态" align="center" prop="activeStatus" >
        <template slot-scope="scope">
        <el-button :type="getAuditStatusText(scope.row.activeStatus) == '启用' ? 'success' : 'danger'" plain>
            {{ getAuditStatusText(scope.row.activeStatus) }}
          </el-button>
       </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.activeStatus == '1' ? true : false"
            icon="el-icon-circle-close"
            @click="handleStaus(scope.row)"
            v-hasPermi="['system:item:edit']"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入服务编码" />
        </el-form-item>
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="服务类型">
        <el-select v-model="form.serveTypeId" clearable placeholder="请选择服务类型">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.activeStatus == 1">
            </el-option>
          </el-select>
      </el-form-item>
        <el-form-item label="服务图标" prop="serveItemIcon">
          <image-upload v-model="form.serveItemIcon"/>
        </el-form-item>
        <el-form-item label="服务图片" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="服务描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="参考价格" prop="referencePrice">
          <el-input v-model="form.referencePrice" placeholder="请输入参考价格" />
        </el-form-item>
        <el-form-item label="排序字段" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入排序字段" />
        </el-form-item>
        <el-form-item label="状态" prop="activeStatus">
        <el-radio-group v-model="form.activeStatus">
          <el-radio label="0">草稿</el-radio>
          <el-radio label="2">启用</el-radio>
          <el-radio label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addItem, delItem, getItem, listItem, updateItem } from "@/api/system/item";
import { listserveType } from "@/api/system/serveType.js";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务项表格数据
      itemList: [],
      //服务类型表格数据
      options:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serveTypeId: null,
        name: null,
        activeStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serveTypeId: [
          { required: true, message: "服务类型id不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "服务名称不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "服务描述不能为空", trigger: "blur" }
        ],
        activeStatus: [
          { required: true, message: "活动状态，0：草稿，1禁用，2启用不能为空", trigger: "change" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getType();
  },
  methods: {
    //状态改变
    getAuditStatusText(status) {
      switch (status) {
        case "2":
          return '启用';
        case "1":
          return '禁用';
        default:
          return '草稿';
      }
    },
    //显示内容
    forTypeName(row,column, value, index){
      return row.serveTypeList[0].name
    },
    /** 查询服务项列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询服务类型
    getType(){
      this.loading = true;
      listserveType().then(response => {
        this.options = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        serveTypeId: null,
        name: null,
        serveItemIcon: null,
        img: null,
        unit: null,
        description: null,
        detailImg: null,
        referencePrice: null,
        sortNum: null,
        activeStatus: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //禁用操作
    handleStaus(row){
      this.reset();
      const ids = row.id;
      this.$modal.confirm('是否确认禁用服务项编号为"' + ids + '"的数据项？').then(() => {
        getItem(ids).then(response => {
          this.form = response.data;
          this.form.activeStatus = 1;
          return updateItem(this.form);
        }).then(() => {
              this.$modal.msgSuccess("禁用成功");
              this.getList();
            }).catch(()=>{});
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务项编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
